//
//  NetworkTools.m
//  NetworkTools
//
//  Created by 方正刚 on 2017/8/25.
//  Copyright © 2017年 方正刚. All rights reserved.
//

#import "NetworkTools.h"

/**
 应用代理调用AFN的私有方法
 */
@protocol NetworkToolsDelegate <NSObject>

@optional
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                       URLString:(NSString *)URLString
                                      parameters:(id)parameters
                                  uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                                downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                         success:(void (^)(NSURLSessionDataTask *, id))success
                                         failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end

@interface NetworkTools ()<NetworkToolsDelegate>

@end

@implementation NetworkTools

+ (instancetype)networkTools {
    
    static NetworkTools *networkTools = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        networkTools = [[NetworkTools alloc] initWithBaseURL:[NSURL URLWithString:@"https://capi.douyucdn.cn/"]];
        
    });
    
    return networkTools;
    
}


- (void)request:(NetworkRequestType)type urlString:(NSString *)urlString parameters:(id)parameters finish:(finished)finish {
    
    
    
    NSString *requestMethod = type == GET ? @"GET" : @"POST";
    
    
    [[self dataTaskWithHTTPMethod:requestMethod URLString:urlString parameters:parameters uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        finish(responseObject, nil);
        
    } failure:^(NSURLSessionDataTask *responseObject, NSError *error) {
        finish(nil, error);
        
    }] resume];
    
    
    //    if (type == GET) {
    //
    //        [self GET:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    //
    //            finish(responseObject, nil);
    //
    //        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    //
    //            finish(nil, error);
    //
    //        }];
    //
    //    }
    //
    //
    //    [self POST:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    //
    //        finish(responseObject, nil);
    //
    //    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    //        
    //        finish(nil, error);
    //
    //    }];
    
    
}





@end
